package com.dayatang.android.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ListView;
import android.widget.Toast;

import com.dayatang.android.Adapter.SearchProjectListViewAdapter;
import com.dayatang.android.Model.Project;
import com.dayatang.android.Task.BaseTask;
import com.dayatang.android.Task.ProjectSearchTask;
import com.dayatang.android.config.Config;

public class ProjectListActivity extends FootCommonActivity {
	
	private ListView listView;
	
    private SearchProjectListViewAdapter simpleAdapter;
    
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AlabSetContentView(R.layout.project_list_activity);
		AlabSetBarTitleText(getString(R.string.project_list));
		InitListView();
	}
	
	private String getIntentExt() {
		return getIntent().getStringExtra(Config.PROJNAME);
	}
	
	private String getUrlExt() {
		return getIntent().getStringExtra(Config.URL);
	}
	
	private void InitListView() {
		listView = (ListView) findViewById(R.id.project_list_listview);
		String result = null;
		try {
			if (getIntentExt().equals("")) {
				String url = getServerAddress() + "/remote/project-advanced-search.action?" + getUrlExt();
				result = new BaseTask(this).execute(url).get();
			} else {
				result = new ProjectSearchTask(this).execute(getServerAddress(),getIntentExt(),Config.BYNAME,getUser().getUsername()).get();
			}
			
			if (result.equals(Config.ERROR)) {
				ToastShow(getString(R.string.serverAddress_alert_addressError));
			} else if (result.equals(Config.NODATA)) {
				ToastShow(getString(R.string.project_no_find_data));
			} else if(result != null) {
				simpleAdapter = new SearchProjectListViewAdapter(this, ProcessData(result));
				listView.setAdapter(simpleAdapter);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
	}
	
	private List<HashMap<String, Object>> ProcessData(String data) {
		List<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
		projectSearchServiceDb.delAllProject();
		try {
			JSONArray jsonArray = new JSONArray(data);
			for (int i = 0; i<jsonArray.length(); i++) {  
				JSONObject jsonObject = (JSONObject) jsonArray.get(i);  
				
				HashMap<String, Object> map = new HashMap<String, Object>();
			    map.put("ItemProjectName",jsonObject.getString("projName"));
			    map.put("ItemProjectState",jsonObject.getString("output")+getString(R.string.subproject_finish_output_unit));
			    map.put("ItemProjectId", jsonObject.getString("id"));
				map.put("ItemProjectBudget", jsonObject.getString("budget")+getString(R.string.subproject_finish_output_unit));
			    
			    Project project = new Project();
			    project.setProjectId(jsonObject.getString("id"));
			    project.setName(jsonObject.getString("projName"));
			    project.setStartDate(jsonObject.getString("startDate"));
			    project.setFinishDate(jsonObject.getString("finishDate"));
			    project.setProjectType(jsonObject.getString("projectType"));
			    project.setArea(jsonObject.getString("area"));
			    project.setResponsibleDivision(jsonObject.getString("responsibleDivision"));
			    project.setBudget(jsonObject.getString("budget"));
			    project.setOutput(jsonObject.getString("output"));
			    project.setExpenditure(jsonObject.getString("expenditure"));
			    project.setOwnerInfo(jsonObject.getString("ownerInfo"));
			    project.setSupervisorInfo(jsonObject.getString("supervisorInfo"));
			    project.setDesignerInfo(jsonObject.getString("designerInfo"));
			    project.setContractNumber(jsonObject.getString("contractNumber"));
			    project.setSubProjectNum(jsonObject.getString("subProjectNum"));
			    project.setSubProjectFinishedNum(jsonObject.getString("subProjectFinishedNum"));
			    project.setSubProjectNoFinishedNum(jsonObject.getString("subProjectNoFinishedNum"));
			    project.setWorkItemQuantity(jsonObject.getString("workItemQuantity"));
			    project.setSpecialtyOutput(jsonObject.getString("specialtyOutput"));
			    
			    projectSearchServiceDb.saveProject(project);
				
			    listItem.add(map);
		    }  
			return listItem;
		} catch (JSONException e) {
			e.printStackTrace();  
			Toast.makeText(this, getString(R.string.project_search_failure), Toast.LENGTH_LONG).show();  
		}
		return listItem;
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			this.finish();
			finishActivitySlide();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
}
